﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using RTCCore;

namespace LibRTC.Core.MessageMap
{
    public class MessagePipline : SimpleMessageHandler
    {
        private LinkedList<IMessageHandler> _handlerList = new LinkedList<IMessageHandler>();

        public MessagePipline(RTC_EVENT rtcEvent, string name) {
            // default value
            _rtcEvent = rtcEvent;
            _name = name;
        }

        public void SetMessageHandlerList(LinkedList<IMessageHandler> list) {
            Debug.Assert(null != list);
            _handlerList = list;
        }

        public override void RegisterHandler(IMessageHandler handler) {
            Debug.Assert(null != handler);
            Debug.Assert(Event() == handler.Event());
            _handlerList.AddLast(handler);
        }

        public override void Process(RTC_EVENT rtcEventType, object rtcEvent)
        {
            // make sure, we deal with the correct event
            Debug.Assert(rtcEventType == _rtcEvent);

            foreach (IMessageHandler handler in _handlerList) {
                Debug.Assert(rtcEventType == handler.Event());
                handler.Process(rtcEventType,rtcEvent);
            }
        }
    }
}
